classdef jwlong_N5227B_class   
    
    properties
        VNA
        idn
    end
  
    properties(Constant)
        e0 = 8.854187817e-12;
        u0 = 4*pi*1e-07;
        c0 = 2.99792458e+08;
    end
    methods
        function obj = jwlong_N5227B_class

        end  
    end
    
    methods(Static)                            
        function [VNA,idn] = InitVNA(company,visa_address)
            % InitVNA 此处显示有关此函数的摘要
            %   此处显示详细说明
            %   初始化方法
            %   VNA = jwlong_N5227B_class;
            %   [VNA.VNA,VNA.idn]=VNA.InitVNA('keysight','TCPIP0::192.168.100.2::5025::SOCKET');
            
            instrreset      
            VNA = visa(company,visa_address);
            fopen(VNA);
            fprintf(VNA,'*IDN?');
            idn = fscanf(VNA);
            fprintf(VNA,'*RST');
            fprintf(VNA,'DISP:WIND 0;');
            fprintf(VNA,'DISP:WIND:STATE ON;');
            fprintf(VNA,'CALC:PAR:DEF ''MyMeas'',S21;');%设置测试S21
            fprintf(VNA,'DISP:WIND:TRAC1:FEED ''MyMeas'';');
            fprintf(VNA,'SENS:SWE:TYPE LIN;');
            fprintf(VNA,'SENS:FREQ:START 2E9');
            fprintf(VNA,'SENS:FREQ:STOP 3E9');  
            fprintf(VNA,['SENS:SWE:POIN ',num2str(401),';']);
            fprintf(VNA,['SENS:BWID ',num2str(10e3),';']);
            fprintf(VNA,['SOUR:POW ',num2str(-10),';']);
            pause(1);
            fprintf(VNA,'INIT:CONT ON');%ON连续模式 OFF为触发模式            
            fprintf(VNA,'DISP:WIND:TRAC1:Y:AUTO');%设置autoscale
%             fprintf(VNA,'INIT:IMMediate');%发送一个触发信号
        end
        
        function SetAutoScale(obj)
            fprintf(obj.VNA,'DISP:WIND:TRAC1:Y:AUTO');%设置autoscale
        end
        
        function SetFreq(obj,setType,freq)
            %   要先初始化之后才能调用 
            %   VNA = jwlong_N5227B_class;
            %   [VNA.VNA,VNA.idn]=VNA.InitVNA('keysight','TCPIP0::192.168.100.2::5025::SOCKET');
            %   VNA.SetFreq(VNA,'start',1e9);
 
            if (strcmp(setType,'start'))||(strcmp(setType,'START'))
                message = ['SENS:FREQ:START ' num2str(freq)];   
            elseif (strcmp(setType,'stop'))||(strcmp(setType,'STOP'))
                message = ['SENS:FREQ:STOP ' num2str(freq)]; 
            elseif (strcmp(setType,'cent'))||(strcmp(setType,'CENT'))
                message = ['SENS:FREQ:CENT ' num2str(freq)]; 
            elseif (strcmp(setType,'span'))||(strcmp(setType,'SPAN'))
                message = ['SENS:FREQ:SPAN ' num2str(freq)];    
            end
            fprintf(obj.VNA,message);
        end       
        
       function SetFreqPoint(obj,freqpoint)
             fprintf(obj.VNA,['SENS:SWE:POIN ',num2str(freqpoint),';']);
        end
        
        function SetIF(obj,ifband)
             fprintf(obj.VNA,['SENS:BWID ',num2str(ifband),';']);
        end
 
        function SetPower(obj,power)
             fprintf(obj.VNA,['SOUR:POW ',num2str(power),';']);
        end
        
        
        function OpenAverage(obj,avgnum)
            fprintf(obj.VNA,'SENS:AVER ON;');
            fprintf(obj.VNA,['SENS:AVER:COUN ',num2str(avgnum),';\n']);
            fprintf(obj.VNA,'SENS:AVER:CLE');
        end      
        
        function OpenSmooth(obj,smoothrate)
            fprintf(obj.VNA,'CALC:SMO ON');
%             fprintf(obj.VNA,['CALC:SMO:POIN ',num2str(smoothrate)]);
            fprintf(obj.VNA,['CALC:SMO:APER ',num2str(smoothrate)]);
        end
        
        function CloseSmooth(obj)
            fprintf(obj.VNA,'CALC:SMO OFF');
        end
        
        function SetMarker(obj,makertype,makernumber)
            %  makertype='OFF' 关闭
            %  makertype='MAX'/ 'MIN' / 'PEAK' 
            %  
            if strcmp(makertype,'OFF')
                fprintf(obj.VNA,['CALC:MEAS:MARK',num2str(makernumber),' OFF;']);
            else
                fprintf(obj.VNA,['CALC:MEAS:MARK',num2str(makernumber),' ON;']);
                fprintf(obj.VNA,['CALC:MEAS:MARK',num2str(makernumber),':FUNC:EXEC ',makertype]); 
            end
        end
        
        function SetMarkerBW(obj,bw)
            fprintf(obj.VNA,'CALC:MEAS:MARK:BWID ON');
            fprintf(obj.VNA,['CALC:MEAS:MARK:BWID:THR ',num2str(bw)]);%设置带宽
            fprintf(obj.VNA,'CALC:MEAS:MARK:FUNC:TRAC ON');
        end
        
        
        
        function [markX,markY] = ReadMark(obj,number)
            clrdevice(obj.VNA);
            message = ['CALC:MARK',int2str(number),':X?'];
            fprintf(obj.VNA,message);
            temp = fscanf(obj.VNA);
            markX = str2double(temp);
            clrdevice(obj.VNA);
            message = ['CALC:MARK',int2str(number),':Y?'];
            fprintf(obj.VNA,message);
            temp = fscanf(obj.VNA);
            MarkY = str2num(temp);
            markY = MarkY(1);
        end
        
        function [fbw,f0,Q0,loss] = ReadQ(obj,bw)
            obj.SetMarkerBW(obj,bw)
            clrdevice(obj.VNA)
            fprintf(obj.VNA,'CALC:MEAS:MARK:BWID:DATA?');
            temp = fscanf(obj.VNA);
            BWdata = str2num(temp);
            fbw = BWdata(1);
            f0  = BWdata(2);
            Q0  = BWdata(3);
            loss= BWdata(4);
        end
        
        function [fbw,f0,Q0,loss] = FindQ(obj,freqcent,freqbw)
            Qbw = [-10,-5,-3.5,-3];
%             Qbw = [-0.3,-0.2,-0.1,-0.05]; %调试的时候用的
            tempfreq = freqcent;
            tempfbw = freqbw;
            for i=1:max(size(Qbw))
                obj.SetFreq(obj,'CENT',tempfreq);
                obj.SetFreq(obj,'SPAN',tempfbw);
                obj.SetMarkerBW(obj,Qbw(i))
                obj.OpenAverage(obj,30);
                obj.OpenSmooth(obj,0.2);
                pause(1);
                obj.SetAutoScale(obj);
                pause(1);
                [fbw,f0,Q0,loss] = obj.ReadQ(obj,Qbw(i));
                fprintf(' Qbw= %.1f,fbw=%.4fMHz,f0=%.4fGHz,Q0=%.1f,loss=%.1f\n',Qbw(i),fbw/1e6,f0/1e9,Q0,loss);
                tempfreq = f0;
                if tempfbw~=0
                    tempfbw = fbw;
                end
            end           
            obj.OpenAverage(obj,1);
            obj.CloseSmooth(obj);
            obj.SetFreq(obj,'SPAN',freqbw);
            obj.SetAutoScale(obj);
        
        end
        
        % 2024年1月5日 添加
        function [VNA,idn] = ConectVNA(company,visa_address)
            % InitVNA 此处显示有关此函数的摘要
            instrreset
            VNA = visa(company,visa_address);
            MAXPOINT = 3001;
            VNA.InputBufferSize = 20*MAXPOINT*2;%读取曲线时，单次传输最大字符数
            fopen(VNA);
            fprintf(VNA,'*IDN?');
            idn = fscanf(VNA);
            fprintf(VNA,'*RST');
            fprintf(VNA,'DISP:WIND 0;');
            fprintf(VNA,'DISP:WIND:STATE ON;');
            fprintf(VNA,'CALC:PAR:DEF ''MyMeas'',S21;');%设置测试S21
            fprintf(VNA,'DISP:WIND:TRAC1:FEED ''MyMeas'';');
            fprintf(VNA,'SENS:SWE:TYPE LIN;');
            pause(1);
            fprintf(VNA,'INIT:CONT ON');%ON连续模式 OFF为触发模式
            fprintf(VNA,'DISP:WIND:TRAC1:Y:AUTO');%设置autoscale
            %             fprintf(VNA,'INIT:IMMediate');%发送一个触发信号
        end
        
        function SetContinus(obj,on_off)
            fprintf(obj.VNA,['INIT:CONT ',on_off]);%ON连续模式 OFF为触发模式
        end
        
        function SendTrig(obj)
            fprintf(obj.VNA,'INIT:IMMediate');%ON连续模式 OFF为触发模式
        end
        
        % 设置测量曲线
        function SetTrace(obj,type)
            fprintf(obj.VNA,'DISP:WIND 0;');
            fprintf(obj.VNA,'DISP:WIND:STATE ON;');
            fprintf(obj.VNA,['CALC:PAR:DEF ''MyMeas'',',type,';']);%设置测试s参数
            fprintf(obj.VNA,'DISP:WIND:TRAC1:FEED ''MyMeas'';');
        end
        % end 2024年1月5日
        
        
    end
    
end